﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Tsanie.DmPoster.Models;
using Tsanie.DmPoster.Utils;

namespace Tsanie.DmPoster {
    public partial class CommentForm : Form {
        internal static CommentForm _commentForm = null;
        private Action<BiliComment> _result = null;
        private MethodInvoker _cancel = null;
        internal BiliComment _comment = null;

        private CommentForm(BiliComment comm) {
            InitializeComponent();
            if (comm == null) {
                comm = new BiliComment();
                comm.Mode = Config.m_mode;
                comm.PlayTime = Config.m_playTime;
                comm.Color = Config.m_color;
                comm.Fontsize = Config.m_fontsize;
            }
            comboMode.SelectedIndex = (comm.Mode == 1 ? 0 : comm.Mode - 3);
            textPlayTime.Text = comm.PlayTime.ToString();
            textColor.Text = StringUtil.StringFromColorInt(comm.Color);
            comboFontsize.Text = comm.Fontsize.ToString();
            this._comment = comm;
        }
        public static void ShowCommentForm(BiliComment comm,
            Action<BiliComment> result, MethodInvoker cancel) {
            if (_commentForm == null) {
                _commentForm = new CommentForm(comm);
                _commentForm._result = result;
                _commentForm._cancel = cancel;
                _commentForm.Location = new Point(
                    Program.m_MainForm.Left + 30,
                    Program.m_MainForm.Top + 30);
                _commentForm.Show(null);
            } else {
                _commentForm.ReadComment(comm);
                _commentForm.Focus();
            }
            _commentForm.textMessage.SelectAll();
        }
        internal void ReadComment(BiliComment comm) {
            if (comm == null)
                return;
            if (this._comment != comm)
                this._comment = comm;
            this.Invoke(new MethodInvoker(() => {
                textPlayTime.Text = comm.PlayTime.ToString();
                textColor.Text = StringUtil.StringFromColorInt(comm.Color);
                comboFontsize.Text = comm.Fontsize.ToString();
                comboMode.SelectedIndex = (comm.Mode == 1 ? 0 : comm.Mode - 3);
                textMessage.Text = comm.Message;
                labelDateTime.Text = "日期：" + comm.Date.ToString("yyyy-MM-dd HH:mm:ss");
                if (comm.User != null && SelectForm.users.ContainsKey(comm.User)) {
                    labelUser.Text = SelectForm.users[comm.User];
                } else {
                    labelUser.Text = comm.User ?? "null";
                }
            }));
        }
        internal bool Save() {
            if (string.IsNullOrEmpty(textMessage.Text)) {
                MessageBox.Show(this, "弹幕内容不可为空！", "编辑", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textMessage.Focus();
                return false;
            }
            if (_comment == null)
                _comment = new BiliComment();
            try {
                float playTime = float.Parse(textPlayTime.Text);
                int color = labelColor.ForeColor.ToArgb() & 0xffffff;
                int fontsize = int.Parse(comboFontsize.Text);
                int mode = (comboMode.SelectedIndex == 0 ? 1 : comboMode.SelectedIndex + 3);
                string message = textMessage.Text;
                if (mode == 7 && _comment.Mode != 7) {
                    // 改为新弹幕
                    BiliPosComment comm = new BiliPosComment();
                    comm.PlayTime = playTime;
                    comm.Color = color;
                    comm.Fontsize = fontsize;
                    comm.Mode = mode;
                    comm.Message = message;
                    _comment = comm;
                    if (_result != null)
                        _result(_comment);
                    return true;
                }
                _comment.PlayTime = playTime;
                _comment.Color = color;
                _comment.Fontsize = fontsize;
                _comment.Mode = mode;
                _comment.Message = message;
                if (_result != null)
                    _result(_comment);
                return true;
            } catch (Exception ex) {
                MessageBox.Show(this, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return false;
        }

        private void CommentForm_FormClosed(object sender, FormClosedEventArgs e) {
            _commentForm = null;
        }
        private void CommentForm_Shown(object sender, EventArgs e) {
            ReadComment(_comment);
        }
        private void textColor_TextChanged(object sender, EventArgs e) {
            try {
                labelColor.ForeColor = StringUtil.ColorFromString(textColor.Text);
            } catch { }
        }
        private void labelColor_Click(object sender, EventArgs e) {
            colorDialog.Color = labelColor.ForeColor;
            if (colorDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) {
                labelColor.ForeColor = colorDialog.Color;
                textColor.Text = StringUtil.StringFromColor(colorDialog.Color);
            }
        }
        private void buttonOk_Click(object sender, EventArgs e) {
            if (Save())
                this.Close();
        }
        private void buttonCancel_Click(object sender, EventArgs e) {
            this.Close();
            if (_cancel != null)
                _cancel();
        }
        private void buttonPreview_Click(object sender, EventArgs e) {
            Save();
        }
    }
}
